package com.simple.elpmobile.onlinecase.ui;

import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.iflytek.elpmobile.utils.IniUtils;
import com.simple.elpmobile.onlinecase.R;
import com.simple.elpmobile.onlinecase.ui.component.FragmetTab1;
import com.simple.elpmobile.onlinecase.ui.component.FragmetTab2;
import com.simple.elpmobile.onlinecase.ui.component.FragmetTab3;

public class MainActivity extends FragmentActivity implements
		OnPageChangeListener, OnClickListener{



	public final static int SUBMIT_HOMEWORK_SUCCESS = 0X70001;
	public final static int CHANGE_TO_SETHOMEWORK = 0x80001;

	private TabPageIndicatorAdapter mAdapter;
	private ViewPager mViewPager;
	private LinearLayout mBtnSetHw;
	private LinearLayout mBntCheckHw;
	private LinearLayout mBtnSeting;
	private int mCurrPageIndex = 0;
	private LinearLayout mCurrBtn;
	private int[] mHovDrawable = { R.drawable.ic_footer2_hov,
			R.drawable.ic_footer1_hov, R.drawable.ic_footer3_hov };
	private int[] mNorDrawable = { R.drawable.ic_footer2_nor,
			R.drawable.ic_footer1_nor, R.drawable.ic_footer3_nor };
	private List<Fragment> mFragments = new ArrayList<Fragment>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.index_layout);
		IniUtils.putBoolean("firstLogin", false);
		mViewPager = (ViewPager) this.findViewById(R.id.hw_pager);
		mAdapter = new TabPageIndicatorAdapter(getSupportFragmentManager());
		mFragments.add(FragmetTab1.newInstance());
		mFragments.add(FragmetTab2.newInstance());
		mFragments.add(FragmetTab3.newInstance());
		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(this);
		mBtnSetHw = (LinearLayout) this.findViewById(R.id.btn_sethw);
		mBntCheckHw = (LinearLayout) this.findViewById(R.id.btn_checkhw);
		mBtnSeting = (LinearLayout) this.findViewById(R.id.btn_seting);
		mCurrBtn = mBtnSetHw;
		mBtnSetHw.setOnClickListener(this);
		mBntCheckHw.setOnClickListener(this);
		mBtnSeting.setOnClickListener(this);
		setBtnState(true, 0);
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}


	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		onBtnSelected(arg0);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_sethw:
			mViewPager.setCurrentItem(0);
			break;
		case R.id.btn_checkhw:
			mViewPager.setCurrentItem(1);
			break;
		case R.id.btn_seting:
			mViewPager.setCurrentItem(2);
			break;
		}
	}

	// ��ťѡ���¼�
	private void onBtnSelected(int pageIndex) {
		if (mCurrPageIndex == pageIndex)
			return;
		setBtnState(false, mCurrPageIndex);
		switch (pageIndex) {
		case 0:
			mCurrBtn = mBtnSetHw;
			break;
		case 1:
			mCurrBtn = mBntCheckHw;
			break;
		case 2:
			mCurrBtn = mBtnSeting;
			break;
		}
		setBtnState(true, pageIndex);
		mCurrPageIndex = pageIndex;
	}

	// ���ð�ť״̬
	private void setBtnState(Boolean selected, int pageIndex) {
		int drawable = mNorDrawable[pageIndex];
		String color = "#4a576c";
		if (selected) {
			drawable = mHovDrawable[pageIndex];
			color = "#009dff";
		}
		View img = mCurrBtn.getChildAt(0);
		if (img instanceof LinearLayout) {
			img.setBackgroundResource(drawable);
		}

		View text = mCurrBtn.getChildAt(1);
		if (text instanceof TextView) {
			((TextView) text).setTextColor(Color.parseColor(color));
		}
	}

	/**
	 * ViewPager������
	 * 
	 * @author Administrator
	 * 
	 */
	private class TabPageIndicatorAdapter extends FragmentPagerAdapter {

		public TabPageIndicatorAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return mFragments.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mFragments.size();
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
		}
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	
}
